package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class StudentServiceImpl implements StudentService {
    /**
     * 分页查询方法
     * @param currentPage 当前页数
     * @param pageSize 每页展示条数
     * @return
     */
    @Override
    public PageInfo<Student> selectByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = null;
        PageInfo<Student> pageInfo = null;
        try {
            //1 加载核心配置文件
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            //2 创建SqlSessionFactory对象
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3 获取SqlSession对象
            sqlSession = factory.openSession();
            //4 获取StudentMapper代理对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //5 执行操作,封装分页结果
            //5.1 设置分页参数
            PageHelper.startPage(currentPage,pageSize);
            //5.2 调用StudentMapper方法
            Page<Student> page = mapper.findAll();
            //5.3 封装分页结果
            pageInfo = new PageInfo<>(page);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            sqlSession.close();
        }
        //6 返回PageInfo
        return pageInfo;
    }

    /**
     * 添加学生信息
     * @param student
     * @return
     */
    @Override
    public int addStudent(Student student) {
        SqlSession sqlSession = null;
        int count = 0 ;
        try {
            //1 加载核心配置文件
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            //2 创建SqlSessionFactory对象
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3 获取SqlSession对象
            sqlSession = factory.openSession(true);
            //4 获取StudentMapper代理对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //5.执行操作
            count = mapper.addStudent(student);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            sqlSession.close();
        }
        return count;
    }

    /**
     * 修改学生的方法
     * @param student
     * @return
     */
    @Override
    public int updateStudent(Student student) {
        SqlSession sqlSession = null;
        int count = 0 ;
        try {
            //1 加载核心配置文件
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            //2 创建SqlSessionFactory对象
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3 获取SqlSession对象
            sqlSession = factory.openSession(true);
            //4 获取StudentMapper代理对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //5.执行操作
            count = mapper.updateStudent(student);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            sqlSession.close();
        }
        return count;
    }

    /**
     * 删除学生的方法
     * @param number
     * @return
     */
    @Override
    public int deleteStudent(String number) {
        SqlSession sqlSession = null;
        int count = 0 ;
        try {
            //1 加载核心配置文件
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            //2 创建SqlSessionFactory对象
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3 获取SqlSession对象
            sqlSession = factory.openSession(true);
            //4 获取StudentMapper代理对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //5.执行操作
            count = mapper.deleteStudent(number);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            sqlSession.close();
        }
        return count;
    }
}
